import {
	defineStore
} from 'pinia'
import {
	getOpenId,
	getChannelList as getChannelListApi,
	getMerchantInfo as getMerchantInfoApi
} from '@/common/api.js'
import {
	getEnv,
} from '@/common/system.js'

export const useCommonStore = defineStore('common', {
	state: () => {
		return {
			openId: '', //获取openId
			openid: '', //获取openId
			merchantInfo: {}, //获取商户信息
			channelList: [], //支付通道列表
			deviceInfo: {}, //码牌信息
			payWay: '', //支付方式
			qrCode: '', //整个二维码信息
		}
	},
	// 也可以这样定义
	// state: () => ({ count: 0 })
	actions: {
		// 获取openId
		getOpenId(data = {}) {
			let _this = this
			return new Promise(async resolve => {
				if (this.openId) {
					resolve(this.openId)
				} else {
					try {
						let res = await getOpenId({
							...data
						})
						if (res.code != 200) {
							uni.showModal({
								content: '获取openId失败',
								showCancel: false,
								success: (res) => {
									if (res.confirm) {
										this.getOpenId({
											...data
										})
									}
								}
							})
							return
						}
						this.openId = res.data.openid
						this.openid = res.data.openid
						resolve(this.openId)
						console.log('获取openid==》', this.openid);
					} catch (error) {
						console.log('请求报错', error);
						//TODO handle the exception
					}
				}
			})
		},
		// 保存码牌信息
		setDeviceInfo(data = {}) {
			this.deviceInfo = data
		},
		// 保存保存二维码信息
		setQrCode(data = {}) {
			this.qrCode = data
		},
		// 获取支付通道列表
		getChannelList() {
			let _this = this
			return new Promise(async (resolve, reject) => {
				// if (this.channelList && this.channelList.length > 0) {
				// 	resolve(this.channelList)
				// } else {
				if (Object.keys(_this.deviceInfo).length == 0) {
					uni.showModal({
						content: '码牌信息不能为空',
						showCancel: false
					})
					return
				}
				let res = []
				try {
					res = await getChannelListApi({
						..._this.deviceInfo
					})
				} catch (error) {
					reject(error)
				}
				if (res.code != 200) {
					uni.showModal({
						content: '获取支付通道失败',
						showCancel: false,
						success: (res) => {
							if (res.confirm) {
								// this.getChannelList()
							}
						}
					})
					return
				}
				let channelMap = {
					'2': {
						payWay: '24',
						name: 'weixin'
					},
					'1': {
						payWay: '3',
						name: 'alipay'
					}
				}
				// #ifdef H5
				if (getEnv() == 'h5-weixin') {
					channelMap['2'].payWay = '23'
				} else if (getEnv() == 'h5-alipay') {
					channelMap['1'].payWay = '1'
				}
				// #endif
				this.channelList = res.data.map(item => {
					return {
						...channelMap[item.businessType],
						...item,
					}
				})
				console.log('this.channelList==>', this.channelList);
				resolve(this.channelList)
				// }
			})
		},
		// 获取商家信息
		getMerchantInfo() {
			let _this = this
			return new Promise(async (resolve, reject) => {
				if (Object.keys(_this.deviceInfo).length == 0) {
					uni.showModal({
						content: '码牌信息不能为空',
						showCancel: false
					})
					return
				}
				let res = []
				try {
					res = await getMerchantInfoApi({
						..._this.deviceInfo
					})
				} catch (error) {
					reject(error)
				}
				if (res.code != 200) {
					uni.showModal({
						content: '获取商家信息失败',
						showCancel: false,
						success: (res) => {
							if (res.confirm) {
								// this.getChannelList()
							}
						}
					})
					return
				}
				this.merchantInfo = {
					...res.data,
					merchantName: res.data.storeAbbrName || res.data.companyAbbrName
				}
				resolve(this.merchantInfo)
			})
		},
		removeMerchantInfo() {
			this.merchantInfo = {}
		},
		// 获取默认支付方式
		getPayWay() {
			// #ifdef MP-ALIPAY
			this.payWay = '3'
			// #endif

			// #ifdef MP-WEIXIN
			this.payWay = '24'
			// #endif

			// #ifdef H5
			if (getEnv() == 'h5-weixin') {
				this.payWay = '23'
			} else if (getEnv() == 'h5-alipay') {
				this.payWay = '1'
			}

			// #endif
			return this.payWay
		},
	},
	unistorage: {
		key: "common", // 缓存的键，默认为该 store 的 id，这里是 common,
		paths: ['openId',
			'openid',
			'deviceInfo',
		], // 需要缓存的路径
	},
	// unistorage: true, // 开启后对 state 的数据读写都将持久化
	// persist: {
	// 	// 调整为兼容多端的API
	// 	storage: {
	// 		setItem(key, value) {
	// 			uni.setStorageSync(key, value) // [!code warning]
	// 		},
	// 		getItem(key) {
	// 			return uni.getStorageSync(key) // [!code warning]
	// 		},
	// 	},
	// 	paths: ['openId', 'openid', 'merchantInfo'],
	// }
})